package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_user_friend_group;

import cn.com.api.service.ApiU_user_friend_groupService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_user_friend_group/")
public class ApiU_user_friend_groupController {
    @Autowired
    private ApiU_user_friend_groupService apiU_user_friend_groupService;
	
	private static Logger logger = Logger.getLogger(ApiU_user_friend_groupController.class);
	
	/**
	 * 新增
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertU_user_friend_group")
	public void insertU_user_friend_group(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String group_name = request.getParameter("group_name");
		String remark = request.getParameter("remark");
		String add_time = request.getParameter("add_time");
		String create_id = request.getParameter("create_id");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiU_user_friend_group u_user_friend_group = new ApiU_user_friend_group();
				
    		    //u_user_friend_group.setId(id);
    		    //u_user_friend_group.setGroup_name(group_name);
    		    //u_user_friend_group.setRemark(remark);
    		    //u_user_friend_group.setAdd_time(add_time);
    		    //u_user_friend_group.setCreate_id(create_id);
				
				apiU_user_friend_groupService.insertU_user_friend_group(u_user_friend_group);
				// return
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_user_friend_groupById")
	public void findU_user_friend_groupById(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    String id = request.getParameter("id");
		    ApiU_user_friend_group u_user_friend_group = apiU_user_friend_groupService.findU_user_friend_groupById(Integer.valueOf(id));
		
			// return
            map.put("datas", u_user_friend_group);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
     /**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_user_friend_groups")
	public void findU_user_friend_groups(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiU_user_friend_group> u_user_friend_groups = apiU_user_friend_groupService.findU_user_friend_groups();
		
			// return
            map.put("datas", u_user_friend_groups);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "updateU_user_friend_group")
	public void updateU_user_friend_group(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String group_name = request.getParameter("group_name");
		String remark = request.getParameter("remark");
		String add_time = request.getParameter("add_time");
		String create_id = request.getParameter("create_id");
        
        
		//String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiU_user_friend_group u_user_friend_group = new ApiU_user_friend_group();
				//获取原数据
				u_user_friend_group  = apiU_user_friend_groupService.findU_user_friend_groupById(Integer.valueOf(id));
				//修改
				//u_user_friend_group.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiU_user_friend_groupService.updateU_user_friend_group(u_user_friend_group);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		}
		catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
    
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "deleteU_user_friend_group")
	public void deleteU_user_friend_group(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
	
				ApiU_user_friend_group group = apiU_user_friend_groupService.findU_user_friend_groupById(Integer.parseInt(id));
				if(!Util.isNull(group))
				{
					if(!group.getGroup_name().equals("我的消息"))
					{
						Integer ret_I = apiU_user_friend_groupService.deleteU_user_friend_group(Integer.parseInt(id));
						if(ret_I >0 ){
		    				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		    				map.put(Utils.MESSAGE, "删除成功！");
		                }else{
						    map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
						    map.put(Utils.MESSAGE, "删除失败！");
					    }
					}
					else {
						map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
	    				map.put(Utils.MESSAGE, "我的消息不可删除！");
					}
				}else {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
    				map.put(Utils.MESSAGE, "分组不存在！");
				}
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
 
}


